package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.PerformanceAppeal;
import com.amsp.oa.oademo.mapper.PerformanceAppealMapper;
import com.amsp.oa.oademo.service.PerformanceAppealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class PerformanceAppealServiceImpl extends ServiceImpl<PerformanceAppealMapper, PerformanceAppeal>
        implements PerformanceAppealService {

    @Override
    public IPage<PerformanceAppeal> getPage(Integer current, Integer size, String name, String department,
            String directSuperior, String handler, String startDate, String endDate) {
        LambdaQueryWrapper<PerformanceAppeal> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        wrapper.like(StringUtils.isNotBlank(name), PerformanceAppeal::getName, name)
                .like(StringUtils.isNotBlank(department), PerformanceAppeal::getDepartment, department)
                .like(StringUtils.isNotBlank(directSuperior), PerformanceAppeal::getDirectSuperior, directSuperior)
                .like(StringUtils.isNotBlank(handler), PerformanceAppeal::getHandler, handler);

        // 处理申诉时间范围
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            wrapper.between(PerformanceAppeal::getAppealDate, startDate, endDate + " 23:59:59");
        }

        // 按申诉时间倒序排序
        wrapper.orderByDesc(PerformanceAppeal::getAppealDate);

        return page(new Page<>(current, size), wrapper);
    }
}